package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.AromaMapper;
import com.dmpp.mes.dao.ModelMapper;
import com.dmpp.mes.parameterInfo.Aroma;
import com.dmpp.mes.parameterInfo.Model;
import com.dmpp.mes.service.AromaService;
import com.dmpp.mes.service.ModelService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "ModelService")

public class ModelServiceImpl implements ModelService {

    @Autowired
    private ModelMapper mapper;

    @Override
    public ResponseData findAll(Model model) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(model));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(Model model) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(model));
        return responseData;
    }

    @Override
    public ResponseData insert(Model model) {
        ResponseData responseData = new ResponseData();
        Model model1= mapper.findByCode(model);
        if (model1==null){
            mapper.insert(model);
        }else {
            responseData.setCodeEnum(RespEnum.ERROR2001);
        }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(Model model) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(model);
        return responseData;
    }

    @Override
    public ResponseData deleteByCode(Model model) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(model);
        return responseData;
    }

    @Override
    public ResponseData delete(Model model) {
        ResponseData responseData = new ResponseData();
        int number = mapper.count(model);
        int productCount=mapper.productCount(model);
        if (model.getMakingsState().equals("原材料")){
            if (number==0){
                mapper.delete(model);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1016);
            }
        }
        if (model.getMakingsState().equals("成品笔")){
            if (productCount==0){
                mapper.delete(model);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1017);
            }
        }


        return responseData;
    }

}
